package KK.Controls.Skin
{
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class SkinSheet implements ISkinSheet
	{
		static private const MAX_GRID:int=3*3;
		
		private var _targetRect:Vector.<Rectangle>;
		private var _area:Vector.<Rectangle>;
		
		private var _tileW:int;
		private var _tileH:int;
		private var offset:Point;
		public function SkinSheet(width:int,height:int)
		{
			
			_tileW=width/3;
			_tileH=height/3;
			init();
		}
		/**
		 *  
		 * 
		 */
		private function init():void{
			offset=new Point();
			_area=new Vector.<Rectangle>;
			calcSheet();
		}
		
		/**
		 * 设置偏移量 
		 * @param value
		 * 
		 */
		public function setOffset(value:Point):void{
			offset=value;
			calcSheet();
		}
		
		/**
		 * 计算片段  
		 * 
		 */
		protected function calcSheet():void{
			
			var col:int=0;
			var row:int=0;
			var MaxCol:int=3;
			var MaxRow:int=3;
			
			for(col=0;col<MaxCol;col++){
				for(row=0;row<MaxCol;row++){
					
					_area[col*MaxCol+row]=(new Rectangle(row*_tileW+offset.x,col*_tileH+offset.y,_tileW,_tileH));
				}
			}
		}
		
		/**
		 * 设置  
		 * @param width
		 * @param height
		 * 
		 */
		public function setSize(size:Point,desPoint:Point):void{
			_targetRect=ConveyScale9(size,_area,desPoint);
		}
		/**
		 * 获取源数据区域  
		 * @return 
		 * 
		 */
		public function get Area():Vector.<Rectangle>
		{
			return _area;
		}
		
		/**
		 * 输出目标信息 
		 * @return 
		 * 
		 */
		public function get targetRect():Vector.<Rectangle>{
			return _targetRect;
		}
		
		
		/**
		 * 赋值一份数据  
		 * @return 
		 * 
		 */
		public function clone():ISkinSheet{
			var result:SkinSheet=new SkinSheet(0,0);
			result._tileH=this._tileH;
			result._tileW=this._tileW;
			result.offset=offset.clone();
			
			var i:int=0;
			var len:int=0;
			if(_area){
				len=_area.length;
				result._area=new Vector.<Rectangle>(MAX_GRID,true);
				
				for(i=0;i<len;i++){
					result._area[i]=_area[i].clone();
				}
			}
			
			if(_targetRect){
				len=_targetRect.length;
				result._targetRect=new Vector.<Rectangle>(MAX_GRID,true);
				
				for(i=0;i<len;i++){
					result._targetRect[i]=_targetRect[i].clone();
				}
			}
			return result;
		}
		
		
		
		/**
		 * 切割成填充区域数据 
		 * 
		 */
		private function ConveyScale9(targetSize:Point,Area:Vector.<Rectangle>,desPoint:Point):Vector.<Rectangle>{
			var targetRect:Rectangle=new Rectangle; 
			var i:int;
			
			var targetList:Vector.<Rectangle>=new Vector.<Rectangle>();
			
			targetRect.y=desPoint.y;
			for(i=0;i<9;i++){
				
				if(i%3){
					targetRect.x+=targetRect.width;
				}else{
					targetRect.x=desPoint.x;
					targetRect.y+=targetRect.height;
				}
				
				switch(true){
					case i/4==1:
						//中间块
						targetRect.width=targetSize.x-Area[i-3].width-Area[i+3].width;
						targetRect.height=targetSize.y-Area[i-3].height-Area[i+3].height; 
						break;
					case Math.floor(i/3)==1:
						//col=1
						targetRect.width=Area[i].width;
						targetRect.height=targetSize.y-Area[i-3].height-Area[i+3].height;
						break;
					case i%2==1:
						//横向填充
						targetRect.height=Area[i].height;
						targetRect.width=targetSize.x-Area[i-1].width-Area[i+1].width;
						break;
					default:
						targetRect.width=Area[i].width;
						targetRect.height=Area[i].height;
						
						break;
				} 
				targetList.push(targetRect.clone()); 
				
			}
			  
			return targetList;
			
			//			FillVectorBitmapData(sourceSkin,skin.bitmapData,sourceList,targetList);
		}
		
	}
}